package job.manager.db.dao;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import job.manager.db.ec.ProfilesEB;

/**
 *  DAO class is responsible for communcation with the
 *  database regarding profiles.
 *  Profiles are the same for customer and employss just a flag
 *  that seperates them.
 */
public class ProfilesDAO {

    private EntityManager em;

    /**
     *  ProfilesDAO
     *
     */
    public ProfilesDAO() {

    }

    public ProfilesDAO(EntityManager em){
        this.em = em;
    }

    public ProfilesEB getProfile(Integer pk){
        return em.find(ProfilesEB.class, pk);
    }

    public int update(ProfilesEB profile){
        if(profile != null){
            ProfilesEB p = em.merge(profile);         
            em.flush();
            return 1;
           
        }
        return 0;
    }
    



    public ProfilesEB insert(ProfilesEB profile){
        if(profile != null){
            em.persist(profile);
            em.flush();
            if( em.contains(profile))
                return profile;
        }
        return null;
    }

    public int delete(ProfilesEB profile){
        if( profile != null) {            
            ProfilesEB p = em.merge(profile);            
            em.remove(p);
            if( !em.contains(p))
                return 1;
        }
        return 0;
    }

    public Collection<ProfilesEB> getAllCustomers() {
        Query q = em.createNamedQuery("ProfilesEB.findByIsCustomer");
        q.setParameter("isCustomer", true);
        return q.getResultList();
    }

    public Collection<ProfilesEB> getAllEmployees() {
        Query q = em.createNamedQuery("ProfilesEB.findByIsEmployee");
        q.setParameter("isEmployee", true);
        return q.getResultList();
    }

    public Collection<ProfilesEB> getAllProfiles() {
        Query q = em.createNamedQuery("ProfilesEB.findAll");
        return q.getResultList();
    }
}
